<?php
namespace OrderAdmin\Controller;
use OrderAdmin\Model\OrderModel;
use OrderAdmin\Model\WorkerModel;
use Think\PageAdmin;

/**
 * 师傅管理控制器
 */
class OrderController extends BaseController
{

    protected $orderModel = null;

    /**
     * 初始化
     *
     * @return [type] [description]
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->orderModel = new OrderModel();
    }

    public function index(){
        $count = $this->orderModel->getAllCount();
        $thisMonthCount = $this->orderModel->getCountThisMonth();
        $doingCount = $this->orderModel->getDoingCount();
        $finishCount = $this->orderModel->getFinishCount();

        //各状态总数统计
        $statusCounts = [];
        $info = $this->orderModel->field('order_status, count(*) count')->group('order_status')->order('order_status asc')->select();
        foreach ($info as $v){
            $statusCounts[$v['order_status']] = $v['count'];
        }
        unset($info);
        $this->assign(compact('count', 'thisMonthCount', 'doingCount', 'finishCount', 'statusCounts'));
        $this->display();
    }

    public function history(){
        $this->display();
    }

    public function getData(){
        $param['worker_id'] = I('post.worker_id', '');
        $param['key'] = I('post.key', '');
        $param['worker_order_status'] = I('post.worker_order_status', 0, 'intval');
        $param['order_status'] = I('post.order_status', '');
        $param['store_id'] = I('post.store_id', '');
        $param['activity_id'] = I('post.activity_id', '');
        $param['search_order_status'] = I('post.search_order_status', '');
        $param['page'] = I('post.page', 1, 'intval');
        $param['size'] = I('post.size', 10, 'intval');
        $data = $this->orderModel->getData($param);
        ajax_return($data['status'], '', $data['data']);
    }

//    public function detail(){
//        $order_no = I('get.order_no', '');
//        if($order_no === ''){
//            $this->error('订单号不存在');
//        }
//        $order_info = M("order")->alias('a')
//            ->field('a.id, a.order_no, a.is_down, a.order_status, a.parter_fee, a.upper_fee,
//            b.store_name, b.manager_name, b.mobile store_mobile, b.address store_address,
//            c.name activity_name, c.up_start_time, c.up_end_time,
//            d.name audit_name, d.mobile audit_mobile,
//            e.name partner_name, e.mobile partner_mobile')
//            ->join('LEFT JOIN __STORE__ b ON a.store_id = b.id')
//            ->join('LEFT JOIN __ACTIVITY__ c ON a.activity_id = c.id')
//            ->join('LEFT JOIN __AUDIT__ d ON a.audit_user_id = d.id')
//            ->join('LEFT JOIN __PARTNER__ e ON a.partner_id = e.id')
//            ->where(['order_no' => $order_no])->find();
//        if(!$order_info){
//            $this->error('订单不存在');
//        }
//
//        $this->order_info = $order_info;
//        $this->seotitle = '订单详情';
//        $this->display();
//    }

    public function detail(){
        $order_no = I('get.order_no', '');
        if($order_no === ''){
            $this->error('订单不存在');
        }
        $data = $this->orderModel->getInfoByOrderNo($order_no);
        if($data){
            $this->seotitle = '订单详情';
            $this->assign(compact('data'));
            $this->display();
        }else{
            $this->error('获取信息失败');
        }
    }

    /**
     * 高空报价
     */
    public function upper_fee(){
        $order_no = I('order_no');
        $parter_fee = I('parter_fee');
        $upper_fee = I('upper_fee');

        $data = ['parter_fee' => $parter_fee, 'upper_fee' => $upper_fee, 'order_status' => 12,
            'upper_fee_time' => time(), 'upper_fee_user' => 1
        ];

        $r = M("order")->where(['order_no' => $order_no])->save($data);
        if ($r) {
            $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '高空报价完成']);
        } else {
            $this->ajaxReturn(['status' => false, 'code' => 1000, 'msg' => '系统错误']);
        }
    }

    /**
     * 删除订单
     */
    public function delete(){
        $id = I('post.id', '');
        if($id === ''){
            $this->error('订单不存在');
        }
        $res = $this->orderModel->deleteById($id);
        $res ? ajax_return(true, '删除成功') : ajax_return(false, '删除失败');
    }

    /**
     * 接受订单
     */
    public function addWorker(){
        $id = I('post.id', '');
        if($id === ''){
            $this->error('订单不存在');
        }
        $param['service_type'] = I('post.service_type', '');
        $param['courier_num'] = I('post.courier_num', '');
        $param['worker_id'] = I('post.worker_id', '');
        $param['up_worker_id_arr'] = I('post.up_worker_id_arr', '');
        $param['down_worker_id'] = I('post.down_worker_id', '');
        $param['down_worker_id_arr'] = I('post.down_worker_id_arr', '');
        if(!$param['service_type'] || !$param['worker_id'] || !$param['down_worker_id']){
            ajax_return(false, '信息不全');
        }
        if($param['up_worker_id_arr']){
            $param['up_worker_id_arr'] = implode(',', $param['up_worker_id_arr']);
        }
        if($param['down_worker_id_arr']){
            $param['down_worker_id_arr'] = implode(',', $param['down_worker_id_arr']);
        }
        $param['worker_order_status'] = '1';
        $res = $this->orderModel->where(['id'=>$id])->save($param);
        $res ? ajax_return(true, '接受订单成功') : ajax_return(false, "接受订单失败");
    }
}